package com.cn.unarmed.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.unarmed.adapter.TestDirection;
import com.cn.unarmed.adapter.WrapScrollLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 利用recyclerView写一个横向滑动列表
 */
public class HoriListScrollViewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view0714);

        initView();
        initData();
    }

    private RecyclerView recyclerView;
    private WrapScrollLayoutManager wrapScrollLayoutManager;
    private List<String> list;

    private void initView() {
        recyclerView = (RecyclerView) this.findViewById(R.id.lay_honey);
        wrapScrollLayoutManager = new WrapScrollLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 11; i++) {
            list.add("第" + i + "个");
        }
        ThisAdapter adapter = new ThisAdapter(this, list);
        recyclerView.setLayoutManager(wrapScrollLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    private class ThisAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

        private List<String> list;
        private LayoutInflater layoutInflater;
        private Context context;

        ThisAdapter(Context context, List<String> strings) {
            this.context = context;
            this.list = strings;
            this.layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = layoutInflater.inflate(R.layout.item_horizontal, parent, false);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((Holder) holder).setText(list.get(list.size() - 1 - position));
            ((Holder) holder).setItemLayout(context, position);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        @Override
        public void onClick(View v) {

        }

        private class Holder extends RecyclerView.ViewHolder {

            private TextView textView;
            private LinearLayout item_horizontal_layout;

            public Holder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(R.id.text_horizontal);
                item_horizontal_layout = (LinearLayout) itemView.findViewById(R.id.item_horizontal_layout);
            }

            public void setText(String x) {
                textView.setText(x);
            }

            public void setItemLayout(Context context, int i) {
                //手动设置layout的宽高
                WindowManager wm = (WindowManager) context
                        .getSystemService(Context.WINDOW_SERVICE);
                int width = wm.getDefaultDisplay().getWidth();
                ViewGroup.LayoutParams params = item_horizontal_layout.getLayoutParams();
                params.width = (width - item_horizontal_layout.getPaddingLeft() - item_horizontal_layout.getPaddingRight()) / 3;
                item_horizontal_layout.setLayoutParams(params);
                if (i % 2 == 0) {
                    item_horizontal_layout.setBackgroundColor(context.getResources().getColor(R.color.bg));
                } else {
                    item_horizontal_layout.setBackgroundColor(context.getResources().getColor(R.color.bg_grey));
                }
            }
        }
    }
}
